package com.spring.sys.service.impl;

import java.util.LinkedHashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.spring.sys.dao.SysUserDao;
import com.spring.sys.po.SysUser;
import com.spring.sys.service.ISysUserService;
import com.spring.util.AliSmsService;
import com.spring.util.EncryptionUtil;
import com.spring.util.IdentityHelper;

@Service
public class SysUserServiceImpl implements ISysUserService {
	protected final static Logger logger = Logger.getLogger(SysUserServiceImpl.class);
	@Autowired
	private SysUserDao sysUserDao;

	@Override
	public SysUser findUserInfoByLoginName(String loginName) {
		// TODO Auto-generated method stub
		return sysUserDao.get("findUserInfoByLoginName", loginName);
	}

	@Override
	public List<SysUser> findAdminUserList(SysUser sysUser) {
		// TODO Auto-generated method stub
		return sysUserDao.find("findAdminUserList", sysUser);
	}

	@Override
	public void updateSysUser(SysUser sysUser) {
		// TODO Auto-generated method stub
		sysUserDao.update("updateByPrimaryKeySelective", sysUser);
	}

	@Override
	public void updatePwd(Integer id) {
		// TODO Auto-generated method stub
		String pwd = new IdentityHelper().randomString(8);
		SysUser sysUser = findById(id);
		sysUser.setPassword(EncryptionUtil.MD5(pwd));
		updateSysUser(sysUser);

		LinkedHashMap<String, String> jsonMap = new LinkedHashMap<String, String>();
		jsonMap.put("password", pwd);
		AliSmsService.sendSMS(sysUser.getMobile(),jsonMap, AliSmsService.ADMIN_RESET_PASSWORD);

	}

	@Override
	public SysUser findById(Integer id) {
		// TODO Auto-generated method stub
		return sysUserDao.get("selectByPrimaryKey", id);
	}

	@Override
	public void insert(SysUser sysUser) {
		// TODO Auto-generated method stub
		sysUserDao.insert(sysUser);
	}

}
